package com.foreknow.tijian.dao.Impl;

import com.foreknow.tijian.dao.SetmealDao;
import com.foreknow.tijian.po.CheckItem;
import com.foreknow.tijian.po.Setmeal;
import com.foreknow.tijian.po.SetmealDetailed;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class SetmealDaoImpl3 implements SetmealDao {
    private Connection con;
    private PreparedStatement pre;
    private ResultSet rs;

    @Override
    public List<Setmeal> getSetmealByType(int type) throws SQLException {
        List<Setmeal> list = new ArrayList<Setmeal>();
        String sql = "select * from setmeal where type=?";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
            pre.setInt(1, type);
            //3.执行查询操作
            rs = pre.executeQuery();
            while (rs.next()) {
                Setmeal setmeal = new Setmeal();
                setmeal.setSmId(rs.getInt("smId"));
                setmeal.setName(rs.getString("name"));
                setmeal.setType(rs.getInt("type"));
                setmeal.setPrice(rs.getInt("price"));
                list.add(setmeal);
//                CheckItem checkItem = new CheckItem();
//                checkItem.


            }
        } finally {
            DBUtil.close(pre);
        }
        return list;
    }

    @Override
    public Setmeal getSetmealById(Setmeal setmeal) throws SQLException {
        /*int smId=setmeal.getSmId();
        Setmeal result=new Setmeal();
        //存储检查项列表
        List<SetmealDetailed> sdList=new ArrayList<>();

       String sql = "SELECT c.ciName,c.ciContent,c.meaning\n" +
               "from setmeal s ,setmealdetailed d,checkitem  c\n" +
               "WHERE s.smId=d.smId and c.ciId=d.ciId  and s.smId=?";*/


        //String sql = "select ?,?,? from setmeal,setmealdetailed,checkitem where setmeal.smId =setmealdetailed.smId and checkitem.ciId=setmealdetailed.ciId  and setmeal.smId=?";
        /*try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
           *//* pre.setString(1, checkItem.getCiName());
            pre.setString(2, checkItem.getCiContent());
            pre.setString(3, checkItem.getMeaning());
            pre.setInt(4, smId);*//*
            pre.setInt(1, smId);
            //3.执行查询操作
            rs = pre.executeQuery();
           // boolean isFirst = true;
            while (rs.next()) {
               // if (isFirst) {
                    result.setSmId(rs.getInt("smId"));
                    result.setName(rs.getString("name"));
                    result.setType(rs.getInt("type"));
                    result.setPrice(rs.getInt("price"));*/

                    /*CheckItem checkItem = new CheckItem();
                    checkItem.setCiId(rs.getInt("ciId"));
                    checkItem.setCiName(rs.getString("ciName"));
                    checkItem.setCiContent(rs.getString("ciContent"));
                    checkItem.setMeaning(rs.getString("meaning"));
                    checkItems.add(checkItem);*/

        //SetmealDetailed setmealDetailed = new SetmealDetailed();
        //  }
        //将检查项列表设置到套餐中
        // result.setSdList();


          /*  }
            for(SetmealDetailed sd: sdList){
                if(sd.getSmId()==smId){
                    result.setSmId(sd.getSmId());

                  }
            }*/
          /* if (rs.next()) {
               setmeal.setSmId(rs.getInt("smId"));
               setmeal.setName(rs.getString("name"));
               setmeal.setSdList();
               setmealDetailed.setCheckItem(checkItem);
           }*/


     /*   } finally {
            DBUtil.close(pre);
        }

        return null;
    }*/
        int smId = setmeal.getSmId();
        Setmeal result = null;
        Map<Integer, SetmealDetailed> sdMap = new HashMap<>(); // 用于明细项去重
        String sql = "SELECT s.smId,s.name, d.sdId,c.ciId,c.ciName,c.ciContent,c.meaning\n" +
                "from setmeal s ,setmealdetailed d,checkitem  c\n" +
                "WHERE s.smId=d.smId and c.ciId=d.ciId  and s.smId=?";

        try {
            con = DBUtil.getConnection();
            pre = con.prepareStatement(sql);
            pre.setInt(1, smId);
            rs = pre.executeQuery();

            while (rs.next()) {
                // 初始化套餐基础信息
                if (result == null) {
                    result = new Setmeal();
                    result.setSmId(rs.getInt("smId"));
                    result.setName(rs.getString("name"));
                    //result.setType(rs.getInt("type"));
                    //result.setPrice(rs.getInt("price"));
                    result.setSdList(new ArrayList<>());
                }

                // 封装明细项（SetmealDetailed）
                Integer sdId = rs.getInt("sdId");
                if (sdId != null && !sdMap.containsKey(sdId)) {
                    SetmealDetailed sd = new SetmealDetailed();
                    sd.setSdId(sdId);
                    sd.setSmId(rs.getInt("smId"));
                    sd.setCiId(rs.getInt("ciId"));

                    // 封装检查项（CheckItem）
                    CheckItem ci = new CheckItem();
                    ci.setCiId(rs.getInt("ciId"));
                    ci.setCiName(rs.getString("ciName"));
                    ci.setCiContent(rs.getString("ciContent"));
                    ci.setMeaning(rs.getString("meaning"));
                    sd.setCheckItem(ci);

                    sdMap.put(sdId, sd);
                    result.getSdList().add(sd);
                }
            }
        } finally {
            DBUtil.close(pre);
        }

        return result;
    }
}
